package com.common.base.app.extras

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：27/6/2025
 *
 * 描述：Bitmap扩展函数：添加圆角和边框
 *
 * 修订历史：
 *
 */
fun Bitmap.addRoundCornersWithBorder(
    cornerRadius: Float = 20f,
    borderWidth: Float = 10f,
    borderColor: Int = Color.WHITE
): Bitmap {
    val width = this.width + (borderWidth * 2).toInt()
    val height = this.height + (borderWidth * 2).toInt()

    val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(output)

    val paint = android.graphics.Paint().apply {
        isAntiAlias = true
        color = borderColor
    }

    // 绘制圆角边框背景
    val rect = android.graphics.RectF(0f, 0f, width.toFloat(), height.toFloat())
    canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint)

    // 创建圆角路径
    val path = android.graphics.Path()
    val innerRect = android.graphics.RectF(
        borderWidth,
        borderWidth,
        width - borderWidth,
        height - borderWidth
    )
    path.addRoundRect(innerRect, cornerRadius, cornerRadius, android.graphics.Path.Direction.CW)

    // 裁剪并绘制原图
    canvas.clipPath(path)
    canvas.drawBitmap(this, borderWidth, borderWidth, null)

    return output
}